package com.ethan.java8;

import com.google.common.math.Stats;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ethan
 * create 2018-08-03 上午11:02
 * desc
 **/
public class Test5 {
    public static void main(String[] args){
        //Stream

        final Collection<Streams.Taskd> tasks = Arrays.asList(
                new Streams.Taskd(Streams.Status.OPEN,5),
                new Streams.Taskd( Streams.Status.OPEN, 13 ),
                new Streams.Taskd( Streams.Status.CLOSED, 8 )
        );

        final long totalPointsOfOpenTasks = tasks.stream()
                .filter(m->m.getStatus() == Streams.Status.OPEN)
                .mapToInt(Streams.Taskd::getPoints)
                .sum();

        System.out.println(totalPointsOfOpenTasks);

        final double totalPoints  = tasks.stream().parallel()
                .map(m->m.getPoints())
                .reduce(0,Integer::sum);


        System.out.println(totalPoints);


        final Map<Streams.Status,List<Streams.Taskd>> map = tasks.stream().collect(Collectors.groupingBy(Streams.Taskd::getStatus));

        System.out.println(map);


        final Collection< String > result = tasks
                .stream()                                        // Stream< String >
                .mapToInt( Streams.Taskd::getPoints )                     // IntStream
                .asLongStream()                                  // LongStream
                .mapToDouble( points -> points / totalPoints )   // DoubleStream
                .boxed()                                         // Stream< Double >
                .mapToLong( weigth -> ( long )( weigth * 100 ) ) // LongStream
                .mapToObj( percentage -> percentage + "%" )      // Stream< String>
                .collect( Collectors.toList() );                 // List< String >

        System.out.println( result );
    }
}


